// ************************* 78 character ruler *****************************
//
// Title:       UnitInformationStorage.c
// Purpose:     A short description of the implementation.
//
// Created on:  2013-3-1 at 15:00:35 by Vinson.Zhang
// Copyright:   dafda. All Rights Reserved.
//
// ************************* 78 character ruler *****************************

#include "RFBB.h"         
#include <utility.h>
#include "stdio.h"
#include<Userint.h>

/* For shell command
_SHMON_UNIT_INFORMATION_STORAGE_CMD g_szUnitInfoStorageCmd[3] ={{"eeprom -s serialNumber %s","eeprom -g serialNumber",'\0'},
                                                                   {"eeprom -s productCode %s","eeprom -g productCode",'\0'},
                                                                   {"eeprom -s testTimestamp %s","eeprom -g testTimestamp",'\0'}};
*/
_FRMON_TEST_STRUCT_CMD g_cFRMONUnitInfoStorageCmd[9] = {{"FRMon,action,load,agent,UnitInfo,Generic,_UnitInfo","done"},
                                                        {"FRMon/Generic/_UnitInfo,action,init,UnitInfo,ASCII","done"},
                                                        {"FRMon/Generic/_UnitInfo,action,write,SERIAL_NO,%s","0x00"},
                                                        {"FRMon/Generic/_UnitInfo,action,read,SERIAL_NO,","\0"},
                                                        {"FRMon/Generic/_UnitInfo,action,write,PRODUCT_CODE,%s","0x00"},
                                                        {"FRMon/Generic/_UnitInfo,action,read,PRODUCT_CODE,","\0"},
														{"FRMon/Generic/_UnitInfo,action,write,TEST_TIMESTAMP,%s","0x00"},
                                                        {"FRMon/Generic/_UnitInfo,action,read,TEST_TIMESTAMP,","\0"},
                                                        {"FRMon/Vegas/_MURKKU2Access,action,terminate,MURKKU2Access","done"}};

void GetTestTimeStampString( char* pszStamp ); 

/**************************************************************************************************
  Function:			UnitInformationStorage
  Description:		The purpose of Store the Unit information to eeprom
  Calls:			none
  Called By:		dll
  Table Accessed:	none
  Table Updated:	none
  Input:			none
  Output:			error code
  Return:			none
  Test Case ID:     SW File activater
  Used Command:		g_cFRMONUnitInfoStorageCmd
**************************************************************************************************/

void __decldllexp TX_TEST UnitInformationStorage(tTestData *testData, tTestError *testError)
{    
    ErrMsg errMsg = {'\0'};
    ERRORINFO errorInfo;
    ErrMsg szLogMsg;
    CAObjHandle hStepName = 0;
    char* pszStepName = NULL;
    char kszShmonAgent[BUFFER_SIZE_128] ={'\0'};
	char kszTemp[BUFFER_SIZE_256] ={'\0'};

    char *pszTemp = NULL;
    char kszSerialNumber[BUFFER_SIZE_64] = {'\0'};
    char kszProductCode[BUFFER_SIZE_64] = {'\0'};
    char kszTimeStamp[BUFFER_SIZE_64] = {'\0'};
    
    char szReturnString[BUF_SIZE_32K + 1] = {'\0'};
    int error = 0;
    int iPassFail = 0;
    int i=0;
    int iPipeNum = 0;
	int iArryNum = 0;
    //////////////////////////////////////////////////////////////////////////   
    // Resolve StepName 
    tsErrChk(TS_SeqContextGetProperty (testData->seqContextCVI, &errorInfo, 
                               TS_SeqContextStep, CAVT_OBJHANDLE, 
                               &hStepName));
    tsErrChk(TS_StepGetProperty (hStepName, &errorInfo, TS_StepName, CAVT_CSTRING, 
                         &pszStepName));

    sprintf( szLogMsg, TEST_NAME_EVENT_FMTSTR, pszStepName );
    ErrChk( EELog_INEventLogLogEvent( g_hEventLog, NULL, szLogMsg ) );

    // Post message to GUI
    tsErrChk(TS_ThreadPostUIMessageEx (g_hThreadPostUIMessage, NULL,
                               TS_UIMsg_ProgressText, 0.0,
                               pszStepName, 0, VTRUE));

    /******************************************Get SerialNumber ***************************************************/
	
    sprintf(kszTemp, "%s.%s.PARAMETERS.SN", TESTPLAN_BASE_PATH, pszStepName );   
    tsErrChk(TS_PropertyGetValString (testData->seqContextCVI, &errorInfo, kszTemp, 0, &pszTemp));
    strcpy(kszSerialNumber, pszTemp);
	//SerialNumber to Read.ReturnValue
	strcpy(g_cFRMONUnitInfoStorageCmd[3].cReturnValue, pszTemp);
	
    /******************************************Get ProductCode ***************************************************/
	sprintf(kszTemp, "%s.%s.PARAMETERS.PRODUCT_CODE", TESTPLAN_BASE_PATH, pszStepName );   
    tsErrChk(TS_PropertyGetValString (testData->seqContextCVI, &errorInfo, kszTemp, 0, &pszTemp));
    strcpy(kszProductCode, pszTemp);
	//ProductCode to Read.ReturnValue
    strcpy(g_cFRMONUnitInfoStorageCmd[5].cReturnValue, pszTemp); 
    
    /******************************************Get system time ***************************************************/
    GetTestTimeStampString( kszTimeStamp );
	//TimeStamp to Read.ReturnValue
	strcpy(g_cFRMONUnitInfoStorageCmd[7].cReturnValue, kszTimeStamp); 
    
	/******************************************TelnetOpen ***************************************************/ 
    //g_hShellCommand = RfbbInetTelnetOpen (g_szDUTIP, g_iShellCommandPort, 0); 
	 g_hFrmonAgent = RfbbInetTelnetOpen (g_szDUTIP, g_iPort, 0);  
	
    /******************************************test ***************************************************/
    
	for(iArryNum = 0;iArryNum < 8;iArryNum++)
    	{
			if(iArryNum == 2 || iArryNum == 4 || iArryNum == 6)
				{   //If Write command, get SerialNumber/ProductCode/TimeStamp from Read.ReturnValue
				sprintf(kszShmonAgent,g_cFRMONUnitInfoStorageCmd[iArryNum].cWriteFrmonCmd,g_cFRMONUnitInfoStorageCmd[iArryNum+1].cReturnValue);
				}
			else
				{
				sprintf(kszShmonAgent,g_cFRMONUnitInfoStorageCmd[iArryNum].cWriteFrmonCmd);    
				}	
			
			//Run FRMon, check Write Command return 0x00, Read Command return SerialNumber/ProductCode/TimeStamp
			FRmonAgent_String(kszShmonAgent, -1,g_cFRMONUnitInfoStorageCmd[iArryNum].cReturnValue, &iPassFail);
        	if (iPassFail!=1) 
        		{
            		goto Error;
        		}
		Delay(1);
    	}    
Error: 
	//action terminate
    sprintf(kszShmonAgent,g_cFRMONUnitInfoStorageCmd[8].cWriteFrmonCmd);
    Frmon_RunCmdNoAck(kszShmonAgent, 1);

#if(ON_OFF != SIMU_VERS)

    if(iPassFail == PASS) 
    	{
        testData->result = PASS;
    	}
    else 
    	{
        testData->result = FAIL;
    	}

   
    //InetTelnetClose(FRCommand)
	InetTelnetClose(g_hFrmonAgent);
	
     //FREE RESOURCES  
    CA_DISCARD_OBJ( hStepName );
	CA_FREE( pszStepName );
	CA_FREE( pszTemp );
	
	//If an error occurred, set the error flag to cause a run-time error in TestStand.
    if(error < 0)
    	{
        testError->errorFlag = TRUE;
        testError->errorCode = error;
        testData->replaceStringFuncPtr(&testError->errorMessage, errMsg);
    	}
    
#else
    testData->result = PASS;
#endif
    // Clear message from GUI
    TS_ThreadPostUIMessageEx (g_hThreadPostUIMessage, NULL,
                                  TS_UIMsg_ProgressText, 0.0, " ", 0, VTRUE);
    
    return;
			 
}
   

// -----------------------------------------------------------------------
// Function:   GetTestTimeStampString  
// Purpose:    Private utility for formatting current date and time to 
//             correct format for test time stamp
// Parameters: char* pszStamp, returns formatted test time stamp

void GetTestTimeStampString( char* pszStamp ) 
{
   // NOTE! pszDate & pszTime:
   // The string returned is a pointer to an internal buffer. 
   char* pszDate; // Do not free this string.
   char* pszTime; // Do not free this string.

   
   pszDate = DateStr();
   pszTime = TimeStr();

   // Format date
   strncpy(pszStamp, pszDate+6, 4);
   strncat(pszStamp, pszDate+3, 2);
   strncat(pszStamp, pszDate, 2);

   // Format time
   strncat(pszStamp, pszTime, 2);
   strncat(pszStamp, pszTime+3, 2);
   strncat(pszStamp, pszTime+6, 2);
   
   // pszDate - Do not free this string.
   // pszTime - Do not free this string.
}


// ************************* 78 character ruler *****************************
//                           End of source file
// **************************************************************************



